package com.gitee.zhanyao.codebase.java.file.controller;

import com.gitee.zhanyao.codebase.java.file.poi.constant.ContentType;
import com.gitee.zhanyao.codebase.java.file.poi.service.WordExportService;
import com.gitee.zhanyao.codebase.java.file.poi.word.WordTemplate;
import com.gitee.zhanyao.codebase.java.file.poi.word.WordUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/export")
public class ExportController {

    @Autowired
    private WordExportService wordExportService;

    @GetMapping("/wordCopy")
    public void wordCopy(HttpServletResponse response) throws Exception {
        String templateFilePath = "classpath:template/国控数据放源模板.docx";
        try {
            XWPFDocument xwpfDocument1 = WordUtil.readFile(templateFilePath);
            XWPFDocument xwpfDocument2 = WordUtil.readFile(templateFilePath);
            WordUtil.appendBody(xwpfDocument1, xwpfDocument2);
            response.setContentType(ContentType.CONTENT_TYPE_DOC);
            response.setHeader("Content-disposition", "attachment; filename=\"" + WordUtil.changeNameToDownload("国控数据放源模板.docx") + "\"");
            InputStream in = WordUtil.getByteArrayInputStream(xwpfDocument1);
            FileCopyUtils.copy(in, response.getOutputStream());
            xwpfDocument1.close();
            xwpfDocument2.close();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @GetMapping("/wordFill")
    public void wordFill(HttpServletResponse response) throws Exception {
        String templateFilePath = "classpath:template/国控数据放源模板.docx";
        try {
            List<Map<String, String>> records = new ArrayList<>();
            Map<String, String> record1 = new HashMap<>();
            record1.put("xxx", "测试aa");
            record1.put("aaa", "测试aaa");
            Map<String, String> record2 = new HashMap<>();
            record2.put("xxx", "测试bb");
            record2.put("aaa", "测试bbb");
            records.add(record1);
            records.add(record2);

            XWPFDocument xwpfDocument = null;
            for (Map<String, String> record: records) {
                XWPFDocument tmpXwpfDocument = WordUtil.readFile(templateFilePath);
                WordTemplate wordTemplate = new WordTemplate(tmpXwpfDocument);
                wordTemplate.replaceTag(record);
                if (xwpfDocument == null) {
                    xwpfDocument = tmpXwpfDocument;
                } else {
                    WordUtil.appendBody(xwpfDocument, tmpXwpfDocument);
                }
            }
            //InputStream in = WordUtil.getByteArrayInputStream(wordTemplate.getXWPFDocument());
            InputStream in = WordUtil.getByteArrayInputStream(xwpfDocument);
            response.setContentType(ContentType.CONTENT_TYPE_DOC);
            response.setHeader("Content-disposition", "attachment; filename=\"" + WordUtil.changeNameToDownload("国控数据放源模板.docx") + "\"");
            FileCopyUtils.copy(in, response.getOutputStream());
            xwpfDocument.close();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

}
